package com.tinyengine.it.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tinyengine.it.common.exception.ServiceException;

import java.io.IOException;
import java.util.Map;

public class JsonValidator extends JsonDeserializer<Map<String, Object>> {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Map<String, Object> deserialize(com.fasterxml.jackson.core.JsonParser p, DeserializationContext ctxt)
            throws IOException {
        try {
            return objectMapper.readValue(p, Map.class);
        } catch (JsonProcessingException e) {
            throw new ServiceException("CM002","参数 不是合法的 JSON");
        }
    }
}
